package com.sqx.modules.shop.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.goods.dao.GoodsShopDao;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.pay.controller.app.AliPayController;
import com.sqx.modules.pay.service.CashOutService;
import com.sqx.modules.pay.service.WxService;
import com.sqx.modules.shop.service.ShopMoneyService;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.sys.dao.SysUserDao;
import com.sqx.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Api(tags={"商户端-商户钱包"})
@RestController
@RequestMapping("/shop/shopmoney")
public class ShopMoneyController extends AbstractController {

    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CashOutService cashOutService;
    @Autowired
    private UserService userService;
    @Autowired
    private WxService wxService;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private AliPayController aliPayController;
    @Autowired
    private GoodsShopDao goodsShopDao;

    @GetMapping("/selectShopMoney")
    @ApiOperation("商户钱包余额")
    public Result selectShopMoney() {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(userEntity.getUserId()));
    }

    @GetMapping("/selectShopMoneyByShopId")
    @ApiOperation("商户钱包余额")
    public Result selectShopMoney(Long shopId) {
        GoodsShop goodsShop = goodsShopDao.selectById(shopId);
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(goodsShop.getUserId()));
    }

    @ApiOperation("商户缴纳保证金")
    @PostMapping(value = "shopCashDeposit")
    public Result shopCashDeposit(String openId, Double money,Integer type) throws Exception {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
//        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        //type作为分类 1 2 3为小程序  4为支付宝app 5为支付宝h5
        if(type==4 || type==5){
            return aliPayController.payShopCashDeposit(sysUserShop.getShopId(), BigDecimal.valueOf(money),type);
        }
        return wxService.shopCashDeposit(sysUserShop.getShopId(), money, openId,type);
    }

    @GetMapping(value = "/shopCashMoney")
    @ApiOperation("商户发起提现")
    public Result shopCashMoney(Double money, Integer classify) {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        return cashOutService.cashMoney(userEntity.getUserId(), sysUserShop.getShopId(),money,classify, 2);
    }

    @GetMapping(value = "shopBindingQrCode")
    @ApiOperation("商户绑定提现二维码")
    public Result shopBindingQrCode(String cashQrCode){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        UserEntity userEntity1 = new UserEntity();
        userEntity1.setUserId(userEntity.getUserId());
        userEntity1.setCashQrCode(cashQrCode);
        userDao.updateById(userEntity1);
        return Result.success();
    }


    @RequestMapping(value = "/selectCashOutList", method = RequestMethod.GET)
    @ApiOperation("查询提现记录列表")
    @ResponseBody
    public Result selectHelpProfit(int page, int limit,Long shopId){

        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        if(shopId!=null){
            GoodsShop goodsShop = goodsShopDao.selectById(shopId);
            map.put("userId",goodsShop.getUserId());
        }else{
            SysUserEntity user = getUser();
            Long adminUserId = user.getUserId();
            UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
            map.put("userId",userEntity.getUserId());
        }
        PageUtils pageUtils = cashOutService.selectCashOutList(map);
        return Result.success().put("data",pageUtils);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation("修改当前登录商户的提现信息")
    public Result updateUser(@RequestBody UserEntity userEntity) {
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        UserEntity userEntity1 = userDao.selectOne(new QueryWrapper<UserEntity>().eq("admin_user_id", adminUserId));
        userEntity.setUserId(userEntity1.getUserId());
        userService.updateById(userEntity);
        return Result.success();
    }

    @RequestMapping(value = "/updateUserByShopId", method = RequestMethod.POST)
    @ApiOperation("修改指定商户的提现信息")
    public Result updateUserByShopId(@RequestBody UserEntity userEntity) {
        GoodsShop goodsShop = goodsShopDao.selectById(userEntity.getShopId());
        userEntity.setUserId(goodsShop.getUserId());
        userService.updateById(userEntity);
        return Result.success();
    }

    @GetMapping(value = "/selectShopMoneydetails")
    @ApiOperation("商户钱包明细")
    public Result selectShopMoneydetails(Integer page, Integer limit, Integer classify, Integer type){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        return userMoneyDetailsService.selectShopMoneyDetails(page, limit, sysUserShop.getShopId(), classify, type);
    }

    @GetMapping(value = "/selectShopMoneydetailsByShopId")
    @ApiOperation("商户钱包明细")
    public Result selectShopMoneydetailsByShopId(Integer page, Integer limit, Integer classify, Integer type,Long shopId){
        return userMoneyDetailsService.selectShopMoneyDetails(page, limit, shopId, classify, type);
    }

    @ApiOperation("商户保证金明细")
    @GetMapping(value = "selectShopCashDeposit")
    public Result selectShopCashDeposit(Integer page, Integer limit, Integer classify){
        SysUserEntity user = getUser();
        Long adminUserId = user.getUserId();
        SysUserShop sysUserShop = sysUserDao.selectUserShop(adminUserId);
        return userMoneyDetailsService.selectShopCashDeposit(sysUserShop.getShopId(), page, limit, classify);
    }

}
